package ThreadPoolCheck.RuleIssue;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;



/**
 * 对于规则引擎中类似问题，加锁无法保证数据正确性，用ThreadLocal可以保证
 * Created by wangyg6 on 2017/11/7.
 */
public class SecondFloor implements Callable<String> {
	private String prefix;
	private int i;
	Lock lock = new ReentrantLock();

	public SecondFloor(String prefix, int i) {
		this.prefix = prefix;
		this.i = i;

	}

	public String call() throws Exception {
		Map map = new HashMap<>();
		map.put("prefix", this.prefix);
		String[] s = new String[100];
		map.put("s",s);
		Thread.sleep((long) Math.random() * FinalConst.BASIC_SLEEP_TIME);
		MapCache.setCache(map);
		System.out.println(MapCache.getCache().get("prefix") + i);
		MapCache.remove();
		return null;

	}
//error
/*	public String call() throws Exception {
				lock.lock();
		MapCache.cacheForTest.put("prefix", i + "");
		Thread.sleep((long) Math.random() * FinalConst.BASIC_SLEEP_TIME);
		System.out.println(MapCache.cacheForTest.get("prefix") + i);
				lock.unlock();
		return null;

	}*/
}
